package com.jihu.design.structural.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jihu
 * @date 2021/9/26
 */
public class Menu {

    private Integer id;
    private String name;
    private Integer level;

    public Menu setLevel(Integer level) {
        this.level = level;
        return this;
    }

    public Menu(Integer id, String name){
        this.id = id;
        this.name = name;
        this.level = 1;
    }

    //组合模式关注点
    private List<Menu> childs = new ArrayList<>();

    //提供添加层级的方法
    void addChildMenu(Menu menu){
        menu.setLevel(this.level + 1);
        childs.add(menu);
    }

    //层级遍历方法
    void printMenu(){
        for (Integer i = 0; i < this.level; i++) {
            System.out.print("\t");
        }
        System.out.println(name);
        if(childs.size() > 0){
            for (Menu child : childs) {
                child.printMenu();
            }
        }

    }

}
